<template>
	<view class="content">
		<view v-if="isLogin">
					<image class="headicon" @click="changeHeadIcon" :src="'http://localhost:8090/foodapp/'+userInfo.icon">
					</image>
					<view class="text-area" v-if="isLogin">
						<text class="title">{{userInfo.nickname}}</text>
					</view>
				</view>
		
				<view v-if="!isLogin">
					<image class="headicon" :src="userInfo.icon"></image>
					<view class="text-area">
						<button type="primary" size="mini" @click="toLogin">点击登录</button>
					</view>
				</view>

		<view style="margin: 20rpx; width: 100%;">
			<view class="entry">个人信息</view>
			<view class="entry">订单列表</view>
			<view class="entry">修改密码</view>
			<view class="entry">意见反馈</view>
		</view>
		<button type="primary" size="default" style="width: 100%;" :disabled="!isLogin" @click="logout">退出</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					userid: null,
					icon: '',
					nickname: '',
					username: null
				},
				isLogin: false
			}
		},
		onLoad() {

		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			changeHeadIcon() {
							let userInfo = uni.getStorageSync('userInfo')
							uni.chooseImage({
								count: 1,
								success: (res) => {
									let tempFilePath = res.tempFilePaths[0]
									uni.uploadFile({
										url: 'http://localhost:8090/foodapp/user/upload_headpic',
										filePath: tempFilePath,
										name: 'file',
										formData: {
											'id': userInfo.id
										},
										success: (res) => {
										    // 更新本地存储，更新页面
											let result = JSON.parse(res.data)
											uni.setStorageSync('userInfo', result.data)
											this.userInfo = result.data
										}
									})
								}
							})
						},
			logout(){
				uni.setStorageSync('userInfo',null)
				this.getUserInfo()
				
			},
			toLogin() {
				uni.navigateTo({
					url: './login'
				})
			},
			getUserInfo() {
				let userInfo = uni.getStorageSync('userInfo')
				if (userInfo) {
					this.userInfo = userInfo
					this.isLogin = true
					console.log('用户已登录，读取成功');
				} else {
					this.userInfo.username = null
					this.userInfo.nickname = null
					this.userInfo.icon = '../../static/my.png'
					this.isLogin = false
				}
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 20rpx;
	}

	.headicon {
		height: 200rpx;
		width: 200rpx;
		margin-top: 80rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		border-radius: 50%;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.entry {
		width: 100%;
		text-align: left;
		height: 3em;
		line-height: 3em;
		text-indent: 1em;
		border-top: #CCC solid 1rpx;
	}

	.entry:last-of-type {
		border-bottom: #CCC solid 1rpx;
	}

	.entry::after {
		content: '';
		background-image: url('../../static/more.png');
		position: absolute;
		width: 50rpx;
		height: 50rpx;
		margin-top: 0.5em;
		background-size: contain;
		right: 50rpx;
	}
</style>